﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace IceDog.Lib.IceDogSocket
{
    
    public class SimpleSocketClientAsync
    {
        private string ip = "127.0.0.1";
        private int port = 8800;

        IPAddress address = null;
        Socket socket = null;

        public SimpleSocketClientAsync()
        {
            address = IPAddress.Parse(ip);
            //step 1 实例化 socket 对象
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(new IPEndPoint(address, port));

            string sendStr = "来自客户端的消息：你好";
            byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
            socket.Send(sendBytes);

            string recStr = "";
            byte[] recBytes = new byte[4096];
            int bytes = socket.Receive(recBytes, recBytes.Length, 0);
            recStr += Encoding.UTF8.GetString(recBytes, 0, bytes);
            Console.WriteLine("客户端获得信息:{0}", recStr);
            socket.Close();
            Console.WriteLine("客户端socket关闭");
        }
    }
}
